package com.ycfmedical.bed.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.local.entity.TimerControl;
import com.ycfmedical.bed.utils.ShareUtil;

import org.greenrobot.greendao.annotation.Entity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.kit.KnifeKit;

/**
 * Created by Administrator on 2017/3/20 0020.
 */
@Entity
public class TimerControlAdapter extends SimpleRecAdapter<TimerControl, TimerControlAdapter.ViewHolder>
{
    public static final int TAG_POST = 0;
    public static final int TAG_TIMES = 1;

    public TimerControlAdapter(Context context)
    {
        super(context);
    }

    @Override
    public ViewHolder newViewHolder(View itemView)
    {
        return new ViewHolder(itemView);
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.adapter_timer_control;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position)
    {
        final  TimerControl item = data.get(position);
        TimerControl timerControl = new TimerControl();
        int postId = 0;
        String times = "";
        String postTv = "";
        // 从share中取出数据
        timerControl = ShareUtil.getInstance().getObject(Constants.CONTROL_TIMING, TimerControl.class);
        if (timerControl != null){
            postId = timerControl.getBodyPost();
            times = timerControl.getTimes();
        }
        switch (postId)
        {
            case 1:
                postTv = "睡眠体位";
                break;
            case 2:
                postTv = "休闲体位";
                break;
            case 3:
                postTv = "就餐体位";
                break;
            case 4:
                postTv = "阅读体位";
                break;

        }
        holder.controlTime.setTag(TAG_TIMES);
        holder.controlTime.setText(times);
        holder.controlContext.setText(postTv);
        // 设置监听事件
        holder.controlContext.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (getRecItemClick() != null)
                {
                    getRecItemClick().onItemClick(position, item, TAG_POST, holder);
                }

            }
        });
        // 设置长按监听事件
        holder.controlTime.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                if (getRecItemClick() != null)
                {
                    getRecItemClick().onItemLongClick(position, item, TAG_TIMES, holder);
                }
                return false;
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {

        @BindView(R.id.control_time)
        TextView controlTime;
        @BindView(R.id.control_context)
        TextView controlContext;

        public ViewHolder(View itemView)
        {
            super(itemView);
            KnifeKit.bind(this, itemView);
        }

        public TextView getControlTime()
        {
            return controlTime;
        }
    }
}
